// pages/profile/profile.js
Page({
  data: {
    userInfo: null,
    hasUserInfo: false,
    statistics: {
      totalApplications: 0,
      acceptedApplications: 0,
      completedJobs: 0,
      totalEarnings: 0,
      // 用工者统计数据
      publishedJobs: 0,
      totalApplicationsReceived: 0,
      activeJobs: 0
    },
    workerMenuItems: [
      {
        icon: '/images/document.png',
        title: '我的资料',
        desc: '完善个人信息',
        url: '/pages/documents/documents'
      },
      {
        icon: '/images/application.png',
        title: '我的申请',
        desc: '查看申请状态',
        url: '/pages/applications/applications'
      },
      {
        icon: '/images/contract.png',
        title: '我的合同',
        desc: '查看合同详情',
        url: '/pages/contracts/contracts'
      },
      {
        icon: '/images/wallet.png',
        title: '我的钱包',
        desc: '收入与提现',
        url: '/pages/wallet/wallet'
      }
    ],
    employerMenuItems: [
      {
        icon: '/images/job.png',
        title: '我的岗位',
        desc: '查看发布的岗位',
        url: '/pages/my-jobs/my-jobs'
      },
      {
        icon: '/images/add-job.png',
        title: '发布岗位',
        desc: '发布新的招聘岗位',
        url: '/pages/publish-job/publish-job'
      },
      {
        icon: '/images/document.png',
        title: '我的资料',
        desc: '完善企业信息',
        url: '/pages/documents/documents'
      },
      {
        icon: '/images/contract.png',
        title: '我的合同',
        desc: '查看合同详情',
        url: '/pages/contracts/contracts'
      },
      {
        icon: '/images/wallet.png',
        title: '我的钱包',
        desc: '账户余额管理',
        url: '/pages/wallet/wallet'
      }
    ],
    commonMenuItems: [
      {
        icon: '/images/settings.png',
        title: '设置',
        desc: '账户与隐私设置',
        url: '/pages/settings/settings'
      },
      {
        icon: '/images/help.png',
        title: '帮助中心',
        desc: '常见问题解答',
        url: '/pages/help/help'
      }
    ]
  },

  onLoad() {
    this.checkLoginStatus();
  },

  onShow() {
    this.checkLoginStatus();
    if (this.data.hasUserInfo) {
      this.loadUserStatistics();
    }
  },

  // 检查登录状态
  checkLoginStatus() {
    const token = wx.getStorageSync('token');
    const userInfo = wx.getStorageSync('userInfo');
    
    if (token && userInfo) {
      this.setData({
        userInfo: userInfo,
        hasUserInfo: true
      });
      this.loadUserStatistics();
    } else {
      this.setData({
        userInfo: null,
        hasUserInfo: false
      });
    }
  },

  // 加载用户统计数据
  loadUserStatistics() {
    const token = wx.getStorageSync('token');
    if (!token) return;

    wx.request({
      url: 'http://localhost:8080/api/user/statistics',
      method: 'GET',
      header: {
        'Authorization': `Bearer ${token}`
      },
      success: (res) => {
        if (res.data.success) {
          this.setData({
            statistics: res.data.data
          });
        }
      },
      fail: () => {
        // 模拟数据
        this.setData({
          statistics: {
            totalApplications: 15,
            acceptedApplications: 8,
            completedJobs: 5,
            totalEarnings: 12500
          }
        });
      }
    });
  },

  // 跳转到登录页面
  goToLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    });
  },

  // 跳转到注册页面
  goToRegister() {
    wx.navigateTo({
      url: '/pages/register/register'
    });
  },

  // 菜单点击处理
  onMenuTap(e) {
    const { page } = e.currentTarget.dataset;
    
    if (page === 'login') {
      wx.navigateTo({
        url: '/pages/login/login'
      });
      return;
    }
    
    if (!this.data.isLoggedIn) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }
    
    // 处理特殊页面跳转
    if (page === '/pages/my-jobs/my-jobs' || page === '/pages/publish-job/publish-job') {
      // 检查用户角色
      if (this.data.userInfo.role !== 'EMPLOYER') {
        wx.showToast({
          title: '仅限用工方使用',
          icon: 'none'
        });
        return;
      }
    }
    
    wx.navigateTo({
      url: page
    });
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          this.setData({
            userInfo: null,
            hasUserInfo: false,
            statistics: {
              totalApplications: 0,
              acceptedApplications: 0,
              completedJobs: 0,
              totalEarnings: 0
            }
          });
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          });
        }
      }
    });
  },

  // 编辑个人信息
  editProfile() {
    wx.navigateTo({
      url: '/pages/edit-profile/edit-profile',
      fail: () => {
        wx.showToast({
          title: '页面开发中',
          icon: 'none'
        });
      }
    });
  },

  // 获取用户角色文本
  getRoleText(role) {
    const roleMap = {
      'WORKER': '劳动者',
      'EMPLOYER': '用工方',
      'ADMIN': '管理员'
    };
    return roleMap[role] || '未知';
  },

  // 获取认证状态文本
  getStatusText(status) {
    const statusMap = {
      'PENDING': '待审核',
      'APPROVED': '已认证',
      'REJECTED': '审核失败',
      'SUSPENDED': '已暂停'
    };
    return statusMap[status] || '未知';
  },

  // 获取认证状态颜色
  getStatusColor(status) {
    const colorMap = {
      'PENDING': '#fa8c16',
      'APPROVED': '#52c41a',
      'REJECTED': '#f5222d',
      'SUSPENDED': '#8c8c8c'
    };
    return colorMap[status] || '#8c8c8c';
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.checkLoginStatus();
    if (this.data.hasUserInfo) {
      this.loadUserStatistics();
    }
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  }
});
